
package com.hlkj.pay.service.common.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.common.constants.RedisConstants;
import com.hlkj.pay.dto.common.*;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.mapper.common.PlatFormConfigMapper;
import com.hlkj.pay.infrastructure.model.common.PlatFormConfigDO;
import com.hlkj.pay.service.common.IPlatFormConfigService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * @author HlpayTeam
 * @date 2024/09/12 15:22
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PlatFormConfigServiceImpl implements IPlatFormConfigService {

    private final PlatFormConfigMapper platFormConfigMapper;

    @Override
    public void saveOrUpdateConfig(PlatFormConfigDO platFormConfigDO) {
        PlatFormConfigDO configDO = queryOne(platFormConfigDO);
        if (configDO != null) {
            configDO.setConfigValue(platFormConfigDO.getConfigValue());
            platFormConfigMapper.updateById(configDO);
        }
        else {
            platFormConfigMapper.insert(platFormConfigDO);
        }
    }

    @Override
    public List<PlatFormConfigDO> platFormConfigList(Integer type) {
        PlatFormConfigDO configDO = new PlatFormConfigDO();
        configDO.setType(type);
        LambdaQueryWrapper<PlatFormConfigDO> queryWrapper = buildWrapper(configDO);
        return platFormConfigMapper.selectList(queryWrapper);
    }

    @Override
    public PlatFormConfigDO platFormConfigByName(String configName) {
        PlatFormConfigDO platFormConfigDO = new PlatFormConfigDO();
        platFormConfigDO.setConfigName(configName);
        return queryOne(platFormConfigDO);
    }

    @Override
    public void savePlatFormConfig(PlatFormConfigDO platFormConfigDO) {
        platFormConfigMapper.insert(platFormConfigDO);
    }

    @Override
    @CacheEvict(value = RedisConstants.PLATFORM_CONFIG, key = "#type")
    public void updatePlatFormConfig(PlatFormConfigDO platFormConfigDO,Integer type) {
        platFormConfigMapper.updateById(platFormConfigDO);
    }

    @Override
    public SmsConfig getSmsConfig() {
        PlatFormConfigDO configDO = queryOne(CommonEnum.CONFIG_TYPE.SMS);
        if (configDO == null) {
            return null;
        }
        return JsonUtils.parseObject(configDO.getConfigValue(), SmsConfig.class);
    }

    @Override
    public GeetestConfig getGeetestConfig() {
        PlatFormConfigDO configDO = queryOne(CommonEnum.CONFIG_TYPE.GEETEST);
        if (configDO == null) {
            return null;
        }
        return JsonUtils.parseObject(configDO.getConfigValue(), GeetestConfig.class);
    }

    @Override
    public OcrConfig getOcrConfig() {
        PlatFormConfigDO configDO = queryOne(CommonEnum.CONFIG_TYPE.OCR);
        if (configDO == null) {
            return null;
        }
        return JsonUtils.parseObject(configDO.getConfigValue(), OcrConfig.class);
    }

    @Override
    public StorageConfig getStorageConfig() {
        PlatFormConfigDO configDO = queryOne(CommonEnum.CONFIG_TYPE.STORAGE);
        if (configDO == null) {
            return null;
        }
        StorageConfig storageConfig = JsonUtils.parseObject(configDO.getConfigValue(), StorageConfig.class);
        if (!storageConfig.getDomain().endsWith("/")) {
            storageConfig.setDomain(storageConfig.getDomain() + "/");
        }
        return storageConfig;
    }

    @Override
    public RsaConfig getRsaConfig() {
        PlatFormConfigDO configDO = queryOne(CommonEnum.CONFIG_TYPE.RSA_CONFIG);
        if (configDO == null) {
            return null;
        }
        return JsonUtils.parseObject(configDO.getConfigValue(), RsaConfig.class);
    }

    @Override
    public DefaultConfig getDefaultConfig() {
        PlatFormConfigDO configDO = queryOne(CommonEnum.CONFIG_TYPE.DEFAULT);
        if (configDO == null) {
            return null;
        }
        return JsonUtils.parseObject(configDO.getConfigValue(), DefaultConfig.class);
    }

    @Override
    public DataFilterConfig getDataFilterConfig() {
        PlatFormConfigDO configDO = queryOne(CommonEnum.CONFIG_TYPE.DATA_FILTER);
        if (configDO == null) {
            return null;
        }
        return JsonUtils.parseObject(configDO.getConfigValue(), DataFilterConfig.class);
    }

    @Override
    public PlatFormConfigDO excelTemplate(Integer type) {
        PlatFormConfigDO platFormConfigDO = new PlatFormConfigDO();
        platFormConfigDO.setType(type);
        return queryOne(platFormConfigDO);
    }

    @Override
    public PlatFormConfigDO bankBranchAppcode() {
        return queryOne(CommonEnum.CONFIG_TYPE.BANK_BRANCH);
    }

    PlatFormConfigDO queryOne(PlatFormConfigDO platFormConfigDO) {
        LambdaQueryWrapper<PlatFormConfigDO> queryWrapper = buildWrapper(platFormConfigDO);
        queryWrapper.last("limit 1");
        return platFormConfigMapper.selectOne(queryWrapper);
    }
    @CacheEvict(value = RedisConstants.PLATFORM_CONFIG, key = "#configType.getType()")
    public PlatFormConfigDO queryOne(CommonEnum.CONFIG_TYPE configType) {
        PlatFormConfigDO platFormConfigDO = new PlatFormConfigDO();
        platFormConfigDO.setConfigName(configType.getCode());
        return queryOne(platFormConfigDO);
    }

    /**
     * 查询条件封装
     * 
     * @param platFormConfigDO
     * @return
     */
    LambdaQueryWrapper<PlatFormConfigDO> buildWrapper(PlatFormConfigDO platFormConfigDO) {
        LambdaQueryWrapperX<PlatFormConfigDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(PlatFormConfigDO::getConfigName, platFormConfigDO.getConfigName());
        queryWrapper.eqIfPresent(PlatFormConfigDO::getType, platFormConfigDO.getType());
        return queryWrapper;
    }
}
